home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE25 / EX18.C < prev    next >
C/C++ Source or Header  |  1995-10-28  |  4KB  |  98 lines

  1. #include <genstub.c>
  2.  
  3. #define IDM_PULSE 500
  4.  
  5. DWORD WINAPI ChildThreadProc( HWND hWnd )
  6. {
  7.    HWND hThreadWnd;             // Handle of window for this thread.
  8.    HANDLE hEvent;               // Handle to event.
  9.    TCHAR szBuffer[101];         // General work area for string formatting.
  10.  
  11.    wsprintf( szBuffer, "Thread %lX", GetCurrentThreadId() );
  12.    hThreadWnd = CreateWindow( lpszAppName, szBuffer, WS_OVERLAPPEDWINDOW, 0, 0,
  13.                               100, 100, (HWND)NULL, NULL, hInst, (LPTSTR)NULL );
  14.    if (!hThreadWnd)
  15.        return (FALSE);
  16.    // Display the window and open the "PULSE" event.
  17.    ShowWindow( hThreadWnd, SW_SHOWNORMAL );
  18.    UpdateWindow( hThreadWnd );
  19.    hEvent = OpenEvent( 0, FALSE, "PULSE" );
  20.    while (TRUE)  // Loop forever going into and out of wait state.
  21.    {
  22.       MSG msg;         // Used for cleaning off queue.
  23.       DWORD dwTest;    // Used for testing the return value of wait function.
  24.       while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
  25.       {
  26.          TranslateMessage( &msg );
  27.          DispatchMessage( &msg );
  28.       }
  29.       wsprintf( szBuffer, "Thread %x waiting for Event %x or Mouse Button Down.",
  30.                 GetCurrentThreadId(), hEvent );
  31.       SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  32.       // Check for manual event or mouse button pressed.
  33.       dwTest = MsgWaitForMultipleObjects( 1, &hEvent, FALSE, 10000, QS_MOUSEBUTTON );
  34.       if (dwTest != WAIT_TIMEOUT)
  35.          wsprintf( szBuffer,"Thread %x satisfied condition.", GetCurrentThreadId() );
  36.       else
  37.          wsprintf( szBuffer,"Thread %x timed out.", GetCurrentThreadId() );
  38.       SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  39.    }
  40.    CloseHandle( hEvent );
  41.    ExitThread( TRUE );
  42. }
  43.  
  44. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  45. {
  46.    static HANDLE hEvent = 0;
  47.    switch ( uMsg )
  48.    {
  49.          case WM_CREATE:
  50.                hEvent = CreateEvent( NULL, TRUE, FALSE, "PULSE" );
  51.                return DefWindowProc( hWnd, uMsg, wParam, lParam );
  52.          case WM_COMMAND:       // process menu items
  53.                switch ( LOWORD( wParam ) )
  54.                {
  55.                      case IDM_TEST:
  56.                      {
  57.                         DWORD dwChildId;
  58.                         CreateThread( NULL, 0, ChildThreadProc, hWnd, 0, &dwChildId );
  59.                      }
  60.                      break;
  61.                      case IDM_PULSE:
  62.                         hEvent = OpenEvent( SYNCHRONIZE, FALSE, "PULSE" );
  63.                         SendMessage( hWnd, WM_USER, 0, (LPARAM)"Pulsing Event" );
  64.                         PulseEvent( hEvent );
  65.                         CloseHandle( hEvent );
  66.                         break;
  67.                      case IDM_EXIT:
  68.                         DestroyWindow( hWnd );
  69.                         break;
  70.                }
  71.          break;
  72.          case WM_USER:
  73.                {  // Show synchronization activity.
  74.                   TCHAR szBuffer[101];
  75.                   static int row = 0;
  76.                   static int msg_num = 1;
  77.                   HDC hDC = GetDC( hWnd );
  78.  
  79.                   FillMemory( szBuffer, 100, 32 );
  80.                   szBuffer[100] = 0;
  81.                   TextOut( hDC, 0, row, szBuffer, lstrlen( szBuffer ) );
  82.                   wsprintf( szBuffer, "%3d: %s", msg_num++, (LPTSTR)lParam );
  83.                   TextOut( hDC, 0, row, szBuffer, lstrlen( szBuffer ) );
  84.                   row = ( row > 220 ) ? 0 : row + 20;
  85.                   ReleaseDC( hWnd, hDC );
  86.                }
  87.                break;
  88.          case WM_DESTROY:
  89.                if ( hEvent )
  90.                   CloseHandle( hEvent );
  91.                PostQuitMessage( 0 );
  92.                break;
  93.          default:
  94.                return DefWindowProc( hWnd, uMsg, wParam, lParam );
  95.    }
  96.    return NULL;
  97. }
  98.